<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'mo:rd10:wmsRd10Apply:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
      <DictLabel v-if="record.status" :dictType="'bpm_biz_status'" :dictValue="record.status" />
    </template>
    <BasicForm @register="registerForm" />
    <template #footer>
      <BpmButton
        v-model:bpmEntity="record"
        bpmEntityKey="id"
        formKey="rd10_apply"
        completeText="提交"
        :loading="loadingRef"
        @complete="handleSubmit"
        @success="handleSuccess"
        @close="closeDrawer"
      />
    </template>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMoRd10WmsRd10ApplyForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import {
    WmsRd10Apply,
    wmsRd10ApplySave,
    wmsRd10ApplyForm,
  } from '/@/api/wms/mo/rd10/wmsRd10Apply';
  import { BpmButton } from '/@/components/Bpm';

  import { DictLabel } from '/@/components/Dict';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('mo.rd10.wmsRd10Apply');
  const { showMessage } = useMessage();
  const record = ref<WmsRd10Apply>({} as WmsRd10Apply);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增产品入库申请单') : t('编辑产品入库申请单'),
  }));

  const bedit = computed(() => (record.value.status === '9' ? false : true));

  const loadingRef = ref(false);

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('订单号'),
      field: 'modid',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      show: false,
    },
    {
      label: t('订单号'),
      field: 'mocode',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      dynamicDisabled: bedit.value,
    },
    {
      label: t('行号'),
      field: 'sortseq',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      dynamicDisabled: bedit.value,
    },
    {
      label: t('部门'),
      field: 'mdeptname',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      dynamicDisabled: bedit.value,
    },
    {
      label: t('存货编码'),
      field: 'invcode',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      dynamicDisabled: bedit.value,
    },
    {
      label: t('存货名称'),
      field: 'basInv.cinvname',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      dynamicDisabled: bedit.value,
    },
    {
      label: t('规格型号'),
      field: 'basInv.cinvstd',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      dynamicDisabled: bedit.value,
    },
    {
      label: t('仓库编码'),
      field: 'cwhcode',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      dynamicDisabled: bedit.value,
    },
    {
      label: t('仓库'),
      field: 'basWare.cwhname',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      dynamicDisabled: bedit.value,
    },
    {
      label: t('生产批次'),
      field: 'cbatch',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      dynamicDisabled: bedit.value,
    },
    {
      label: t('数量'),
      field: 'iqty',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: t('请输入一个数值') }],
      dynamicDisabled: bedit.value,
    },
    {
      label: t('备注'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 1000,
      },
      colProps: { lg: 24, md: 24 },
      dynamicDisabled: bedit.value,
    },
    {
      label: t('检验数量'),
      field: 'checkQty',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: t('请输入一个数值') }],
      dynamicDisabled: record.value.status === '0',
      ifShow: record.value.status !== '9',
    },
    {
      label: t('入库数量'),
      field: 'rkQty',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: t('请输入一个数值') }],
      ifShow: record.value.status !== '9',
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await wmsRd10ApplyForm(data);
    record.value = (res.wmsRd10Apply || {}) as WmsRd10Apply;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      data.bpm = record.value.bpm; // 流程信息
      data.status = record.value.status; // 提交状态
      loadingRef.value = true;
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        code: record.value.code,
      };
      // console.log('submit', params, data, record);
      const res = await wmsRd10ApplySave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      loadingRef.value = false;
      setDrawerProps({ confirmLoading: false });
    }
  }

  async function handleSuccess() {
    emit('success');
  }
</script>
